//
//  BNRHypnosisViewController.m
//  Quiz
//
//  Created by YaoJacob on 15/11/2.
//  Copyright © 2015年 Roger. All rights reserved.
//

#import "BNRHypnosisViewController.h"
#import "BNRHypnosisView.h"
#import "BNRTextFiledProtocol.h"

@interface BNRHypnosisViewController () <UITextFieldDelegate>

@end

@implementation BNRHypnosisViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarItem.title = @"晕了";
        self.tabBarItem.image = [UIImage imageNamed:@"Hypno"];
    }
    return self;
}

-(void)loadView
{
    BNRHypnosisView *view = [[BNRHypnosisView alloc]init];
    self.view = view;
    
    CGRect textFieldRect = CGRectMake(40, 70, 240, 30);
    UITextField *textFiled = [[UITextField alloc]initWithFrame:textFieldRect];
    textFiled.borderStyle = UITextBorderStyleRoundedRect;
    textFiled.placeholder = @"input some tips.";
    textFiled.returnKeyType = UIReturnKeyDone;
    
    //first  成功
    textFiled.delegate = self;  //ok.
    
    //second 失败？？？
    //    BNRTextFiledProtocol *txtProtocol = [[BNRTextFiledProtocol alloc]init];
    //    textFiled.delegate = txtProtocol;
    [view addSubview:textFiled];
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"%@ : %@", self, @"viewDidLoad");
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"%@ : %@", self, @"viewDidAppear");
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"%@ : %@", self, @"viewWillAppear");
    
}

-(BOOL)textFieldShouldReturn:(UITextField*)textField
{
    NSLog(@"BNRHypnosisViewController - %@", textField.text);
    
    [self drawHypnoticMessage:textField.text];
    textField.text = @"";
    [textField resignFirstResponder];
    return YES;
}

-(void)drawHypnoticMessage:(NSString*)message
{
    for (int i = 0; i < 20; i++) {
        UILabel * msgLabel = [[UILabel alloc]init];
        msgLabel.backgroundColor = [UIColor clearColor];
        msgLabel.textColor = [UIColor whiteColor];
        msgLabel.text = message;
        
        [msgLabel sizeToFit];
        
        int width = (int)(self.view.bounds.size.width - msgLabel.bounds.size.width);
        int x = arc4random() % width;
        
        int height = (int)(self.view.bounds.size.height - msgLabel.bounds.size.height);
        int y = arc4random() % height;
        
        CGRect frame = msgLabel.frame;
        frame.origin = CGPointMake(x, y);
        msgLabel.frame = frame;
        
        [self.view addSubview:msgLabel];
        
        //Motion
        UIInterpolatingMotionEffect *motionEffect;
        //x
        motionEffect = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
        motionEffect.minimumRelativeValue = @(-100);
        motionEffect.maximumRelativeValue = @(100);
        [msgLabel addMotionEffect:motionEffect];
        
        //y
        motionEffect = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
        motionEffect.minimumRelativeValue = @(-100);
        motionEffect.maximumRelativeValue = @(100);
        [msgLabel addMotionEffect:motionEffect];
    }
}

@end
